const axios = require('axios');
const xlsx = require('node-xlsx');
var fs = require('fs');
var moment = require('moment');

console.log('process.argv[i]:',process.argv[2])

axios.get('http://172.50.100.183/prod-api/jhapi/fa/order/dataSupplementationV2',{
    headers:{
        'authorization':'bearer '+process.argv[2]
    }
})
  .then(response => {
    console.log('2222')
    console.log(response.data);
    if(response.data.code!=2000) return;
    const data = response.data.data;

    const tableData = [['类别','总数','已完成数','已完成百分比','每人完成进度']];
for(var key in data){
    if(key.includes('===')) continue;
  //  console.log('data[key]:',data[key])
    const rowData = data[key].split('  ').filter(s=>s.trim().length>0);
 //   console.log('rowData:',rowData)
    
    const columns = rowData[0].split('/')
    const lastCol = rowData[1].split('[')[1].replace(']','');
    const parts = lastCol.match(/.{1,80}(?:\s|$)/g) || [];
    // 使用换行符连接分割后的部分
    
    tableData.push(
        [
            key.replace(':',''),columns[1],columns[0],
            (parseFloat(columns[0])/parseFloat(columns[1])*100).toFixed(2)+'%'
            , parts.join('\n')
        ]
    )
}
const gm = [
      tableData.slice(1).filter(s=>s[0].includes('钢贸'))
        .map(s=>{
            if(s[1].includes('(')) return s[1].split('(')[0]
            return s[1]
            })
            .reduce((a,b)=>Number(a)+Number(b)),
        tableData.slice(1).filter(s=>s[0].includes('钢贸'))
        .map(s=>{
            if(s[2].includes('(')) return s[2].split('(')[0]
            return s[2]
            })
        .reduce((a,b)=>Number(a)+Number(b)),
]
const guom = [
     tableData.slice(1).filter(s=>s[0].includes('国贸'))
        .map(s=>{
            if(s[1].includes('(')) return s[1].split('(')[0]
            return s[1]
            })
        .reduce((a,b)=>Number(a)+Number(b)),
        tableData.slice(1).filter(s=>s[0].includes('国贸'))
        .map(s=>{
            if(s[2].includes('(')) return s[2].split('(')[0]
            return s[2]
            })
        .reduce((a,b)=>Number(a)+Number(b)),
]
tableData.push(
    ['钢贸合计',
        ...gm,
        (parseFloat(gm[1])/parseFloat(gm[0])*100).toFixed(2)+'%',
        ''
    ],
     ['国贸合计',
       ...guom,
        (parseFloat(guom[1])/parseFloat(guom[0])*100).toFixed(2)+'%',
        ''
    ]
)
//console.log('tableData:',tableData)

const sheetOptions = {
    '!cols': [
        { wch: 20 },
        { wch: 20 },
        { wch: 20 },
        { wch: 20 },
        { wch: 100 },
    ]
};


axios.get('http://172.50.100.183/prod-api/jhapi/fa/order/dayV5',{
    headers:{
        'authorization':'bearer '+process.argv[2]
    }
})
  .then(response => {
    if(response.data.code!=2000) return;
    const d = response.data.data;

    console.log('d:',d)

    
const nmM =  d['内贸新产销主档'].split('(')
const nmD =  d['内贸新产销子档'].split('(')

const wmM =  d['外贸新产销主档'].split('(')
const wmD =  d['外贸新产销子档'].split('(')

const clnm =  d['存量内贸新产销主档'].split('  ')
const clnz =  d['存量内贸新产销子档'].split('  ')


const clnmW =  d['存量外贸新产销主档'].split('  ')
const clnzW =  d['存量外贸新产销子档'].split('  ')

const tableData2 = [
        ['',
            '老产销中间表','老产销中间表','新产销业务表 ','新产销业务表','差异数','差异数'
        ],
        ['','累计主档','累计子项','累计主档'	,'累计子项',	'主档差异'	,'子项差异',],
        [
            '内贸',
            d['内贸老产销中间表主档'],
            d['内贸老产销中间表子档'],
            nmM[0].trim(),nmD[0].trim(),nmM[1].replace(/[^\\d]/g, ""),nmD[1].replace(/[^\\d]/g, ""),
        ],
        [
            '外贸',
            d['外贸老产销中间表主档'],d['外贸老产销中间表子档'],wmM[0].trim(),wmD[0].trim(),wmM[1].replace(/[^\\d]/g, ""),
            wmD[1].replace(/[^\\d]/g, ""),],

        ['','当日新增主档','当日新增子项','当日新增主档','当日新增子项','','',],
        [
             '内贸',
            d['存量内贸老产销中间表主档'].replace(/[^\\d]/g, ""),
            d['存量内贸老产销中间表子档'].replace(/[^\\d]/g, ""), 
         clnm[1],clnz[1],
          '','',],


        [
             '外贸',
            d['存量外贸老产销中间表主档'].replace(/[^\\d]/g, ""),
            d['存量外贸老产销中间表子档'].replace(/[^\\d]/g, ""), 
         clnmW[1],clnzW[1],
          '','']
]
// console.log('tableData:',tableData)
// console.log('tableData2:',tableData2)


var buffer = xlsx.build([
    {name:'实时统计',data:tableData,options:sheetOptions},
     {name:'每日统计',data:tableData2,options:
            {
                '!cols': [
                    { wch: 20 },
                    { wch: 20 },
                    { wch: 20 },
                    { wch: 20 },
                    { wch: 20 },
                ],
                '!merges':[
                    { s: { c: 1, r: 0 }, e: { c: 2, r: 0 } },
                    { s: { c: 3, r: 0 }, e: { c: 4, r: 0 } },
                    { s: { c: 5, r: 0 }, e: { c: 6, r: 0 } }
                ]
            }


     }
]);
const date = moment().format('YYYYMMDDHHmmss');
 fs.writeFileSync('./统计'+date+'.xlsx',buffer,{'flag':'w'});})

  })



  .catch(error => {
    console.log(error);
  }


);

// const data = {
//     "钢贸主档:": "1258 / 1261    钢贸主档需补录情况:[白高峰:1, 崔斌:1, 李孟兴:1]",
//         "国贸内贸主档:": "60 / 60     国贸内贸主档需补录情况:[]",
//         "国贸外贸主档完成情况:": "285 / 303   已补录明细:[陈修存:12/12, 索毅:10/10, 胡嘉伟:47/47, 范志中:23/23, 王海铭:18/18, 李宝亮:14/14, 顾佳:3/3, 杨洋:0/1, 李少波:3/3, 周康宁:7/7, 张奕:2/2, 邢楠:19/20, 李欧:5/5, 杨勇强:5/11, 丛超:12/12, 董天娇:0/1, 罗振兴:17/17, 秦川:7/16, 张新:9/9, 胡玮:2/2, 牛凯峰:10/10, 吴迪:18/18, 曾淼:2/2, 雷雨顺:28/28, 武尚:10/10, 张斌:2/2]",
//         "==================": "==========================",
//         "钢贸子档:": "4574 / 4575   钢贸子档需补录情况:[李孟兴:1]",
//         "国贸内贸子档完成情况:": "486 / 486  已补录明细:[丛超:17/17, 周康宁:3/3, 张奕:1/1, 张斌:119/119, 曾淼:22/22, 李宝亮:11/11, 李少波:5/5, 武尚:144/144, 索毅:35/35, 胡嘉伟:39/39, 董天娇:72/72, 邢楠:18/18]",
//         "国贸外贸子档完成情况:": "3633(2539+1094)/3878(2784+1094)    已补录明细:[丛超:70(54+16)/71(55+16), 吴迪:396(272+124)/396(272+124), 周康宁:22(22+0)/27(27+0), 张奕:2(2+0)/2(2+0), 张斌:5(5+0)/5(5+0), 张新:16(16+0)/16(16+0), 曾淼:9(9+0)/9(9+0), 李宝亮:43(43+0)/43(43+0), 李少波:22(22+0)/22(22+0), 李欧:496(55+441)/656(215+441), 杨勇强:42(19+23)/44(21+23), 杨洋:0(0+0)/1(1+0), 武尚:73(73+0)/83(83+0), 牛凯峰:33(33+0)/33(33+0), 王海铭:71(71+0)/71(71+0), 秦川:133(50+83)/174(91+83), 索毅:84(12+72)/101(29+72), 罗振兴:125(125+0)/125(125+0), 胡嘉伟:342(261+81)/347(266+81), 胡玮:178(155+23)/178(155+23), 范志中:549(549+0)/549(549+0), 邢楠:315(288+27)/315(288+27), 陈修存:329(157+172)/332(160+172), 雷雨顺:223(222+1)/223(222+1), 顾佳:50(24+26)/50(24+26)]"
// }


